package com.sky.youxue.web;

import com.sky.youxue.common.enums.ExceptionEnum;
import com.sky.youxue.common.exception.youxueException;
import com.sky.youxue.common.utils.CookieUtils;
import com.sky.youxue.config.JwtProperties;
import com.sky.youxue.entity.UserInfo;
import com.sky.youxue.service.AuthService;
import com.sky.youxue.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: youxue-auth-center
 * @description: 授权Controller
 * @author: Mr.Gouyu
 * @create: 2019-01-08 11:07
 **/

@RestController
@EnableConfigurationProperties(JwtProperties.class)
public class AuthController {

    @Autowired
    private AuthService authService;

    @Autowired
    private JwtProperties prop;

    /**
     * 登录授权
     * @param username
     * @param password
     * @param request
     * @param response
     * @return
     */

    @PostMapping("login")
    public ResponseEntity<Void> authentication(
            @RequestParam("username") String username ,
            @RequestParam("password") String password ,
            HttpServletRequest request ,
            HttpServletResponse response){
        String token = this.authService.authentication(username,password) ;
        System.out.println(token);
        // 将token写入cookie,并指定httpOnly为true，防止通过JS获取和修改
        CookieUtils.setCookie(request,response ,prop.getCookieName(),
                token, prop.getCookieMaxAge(), "utf-8");
//      System.out.println(CookieUtils.getCookieValue(request,"YX_TOKEN").toString());
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 验证用户信息
     * @param token
     * @param request
     * @param response
     * @return
     */
    @GetMapping("verify")
    public ResponseEntity<UserInfo> verifyUser(@CookieValue("YX_TOKEN") String token ,HttpServletRequest request, HttpServletResponse response){
        try {
//            String token = "eyJhbGciOiJSUzI1NiJ9.eyJpZCI6NDAsInVzZXJuYW1lIjoid2FuZ2RvbmdsZWkxIiwiZXhwIjoxNTQ3NzEwNTkwfQ.RldO6qS2hzEYM7xM9D3b6F6ShVWa-H0Nhv0JlmMuW7n_P3JL7XKTRc4QmNcXVPPOJ9x2Zy5dErzBFp3N0JLR5b4mWC07qYpWFK1VyWj2_bffjYmq0lLvWngajUpfBr-ZG42pNyGJoRZ2NqJQfYptKc5wZhneQOEH24gz7kCfF-E";
            UserInfo userInfo = JwtUtils.getInfoFromToken(token,prop.getPublicKey());
            String newToken = JwtUtils.generateToken(userInfo,
                    prop.getPrivateKey() ,prop.getExpire());

            CookieUtils.setCookie(request,response,prop.getCookieName(),newToken,prop.getCookieMaxAge(),"utf-8");
            System.out.println("userInfo"+userInfo.getUsername());
            return ResponseEntity.ok(userInfo);
        } catch (Exception e) {
            throw new youxueException(ExceptionEnum.UNAUTHORIZED);
        }
    }
}
